package com.meixiang.fragment.account;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.manager.VerifyRealNameActivity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.tool.Tool;
import com.meixiang.util.KeyBoardUtils;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 实名认证——实名认证
 *
 * @author LZRUI
 */
public class VerifyRealNameFragment extends BaseFragment {

    @Bind(R.id.edtTxt_name)
    ClearEditText edtTxtName; // 姓名
    @Bind(R.id.edtTxt_card_num)
    ClearEditText edtTxtCardNum; // 证件号
    @Bind(R.id.btn_verify)
    CheckEditTextEmptyButton btnVerify; // 认证按钮

    public VerifyRealNameFragment() {

    }

    public static VerifyRealNameFragment newInstance() {
        return new VerifyRealNameFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_verify_real_name, container, false);
        ButterKnife.bind(this, rootView);
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        btnVerify.setEditText(new ClearEditText[]{edtTxtName, edtTxtCardNum});
    }

    @Override
    protected void setData() {

    }

    @OnClick(R.id.btn_verify)
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        // 认证提交
        if (view.getId() == R.id.btn_verify) {
            submit();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        KeyBoardUtils.hideSoftInput(edtTxtCardNum);
    }

    private void submit() {
        HttpParams params = new HttpParams();
        params.put("idCard", edtTxtCardNum.getText().toString().trim());
        params.put("name", edtTxtName.getText().toString().trim());
        HttpUtils.post(Config.REALNAME_AUTHENTICATION, VerifyRealNameActivity.TAG, params, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
                KeyBoardUtils.hideSoftInput(edtTxtCardNum);
                activity.finish();
            }

            @Override
            public void onError(String code, String message) {
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
